package o;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.net.Socket;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import okhttp3.Route;
import okhttp3.internal.connection.RealConnection;
import okhttp3.internal.connection.StreamAllocation;
import okhttp3.internal.connection.StreamAllocation.e;

public final class fuf {
    private static final Executor ˊ = new ThreadPoolExecutor(0, ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED, 60, TimeUnit.SECONDS, new SynchronousQueue(), fvg.ˊ("OkHttp ConnectionPool", true));
    static final /* synthetic */ boolean ॱ = (!fuf.class.desiredAssertionStatus());
    private final Deque<RealConnection> ʻ;
    private final long ʼ;
    private final Deque<fuq> ʽ;
    boolean ˋ;
    private final int ˎ;
    final fvn ˏ;
    private final Deque<WeakReference<d>> ॱॱ;
    private final Runnable ᐝ;

    public interface d {
        void ॱ(String str, int i, String str2);
    }

    public fuf() {
        this(5, 5, TimeUnit.MINUTES);
    }

    public fuf(int i, long j, TimeUnit timeUnit) {
        this.ᐝ = new Runnable(this) {
            final /* synthetic */ fuf ˏ;

            {
                this.ˏ = r1;
            }

            public void run() {
                while (true) {
                    long ˎ = this.ˏ.ˎ(System.nanoTime());
                    if (ˎ != -1) {
                        if (ˎ > 0) {
                            long j = ˎ / 1000000;
                            ˎ -= j * 1000000;
                            synchronized (this.ˏ) {
                                try {
                                    this.ˏ.wait(j, (int) ˎ);
                                } catch (InterruptedException e) {
                                }
                            }
                        }
                    } else {
                        return;
                    }
                }
            }
        };
        this.ʻ = new ArrayDeque();
        this.ˏ = new fvn();
        this.ʽ = new ArrayDeque();
        this.ॱॱ = new ArrayDeque();
        this.ˎ = i;
        this.ʼ = timeUnit.toNanos(j);
        if (j <= 0) {
            throw new IllegalArgumentException("keepAliveDuration <= 0: " + j);
        }
    }

    @Nullable
    RealConnection ॱ(ftt o_ftt, StreamAllocation streamAllocation, Route route) {
        if (ॱ || Thread.holdsLock(this)) {
            RealConnection ॱ = ॱ(o_ftt);
            if (ॱ != null) {
                streamAllocation.acquire(ॱ, true);
                return ॱ;
            }
            for (RealConnection ॱ2 : this.ʻ) {
                if (ॱ2.isEligible(o_ftt, route)) {
                    streamAllocation.acquire(ॱ2, true);
                    return ॱ2;
                }
            }
            return null;
        }
        throw new AssertionError();
    }

    @Nullable
    Socket ˎ(ftt o_ftt, StreamAllocation streamAllocation, int i) {
        if (ॱ || Thread.holdsLock(this)) {
            int i2 = 0;
            for (RealConnection realConnection : this.ʻ) {
                if (realConnection.isEligible(o_ftt, null) && realConnection.isMultiplexed() && realConnection != streamAllocation.connection()) {
                    i2++;
                    if (i2 == i) {
                        return streamAllocation.releaseAndAcquire(realConnection);
                    }
                }
                i2 = i2;
            }
            return null;
        }
        throw new AssertionError();
    }

    void ˎ(RealConnection realConnection) {
        if (ॱ || Thread.holdsLock(this)) {
            if (!this.ˋ) {
                this.ˋ = true;
                ˊ.execute(this.ᐝ);
            }
            this.ʻ.add(realConnection);
            if (realConnection.isMultiplexed()) {
                ˋ(realConnection);
                return;
            }
            return;
        }
        throw new AssertionError();
    }

    boolean ˏ(RealConnection realConnection) {
        if (!ॱ && !Thread.holdsLock(this)) {
            throw new AssertionError();
        } else if (realConnection.noNewStreams || this.ˎ == 0) {
            this.ʻ.remove(realConnection);
            ॱ(realConnection);
            return true;
        } else {
            notifyAll();
            return false;
        }
    }

    long ˎ(long j) {
        RealConnection realConnection = null;
        long j2 = Long.MIN_VALUE;
        synchronized (this) {
            int i = 0;
            int i2 = 0;
            for (RealConnection realConnection2 : this.ʻ) {
                long j3;
                if (ˊ(realConnection2, j) > 0) {
                    i++;
                } else if (!realConnection2.isMultiplexed() || j - realConnection2.keepaliveTimestampNs >= RealConnection.MAX_RESERVE_DURATION_NS) {
                    RealConnection realConnection3;
                    int i3 = i2 + 1;
                    long j4 = j - realConnection2.idleAtNanos;
                    if (j4 > j2) {
                        long j5 = j4;
                        realConnection3 = realConnection2;
                        j3 = j5;
                    } else {
                        realConnection3 = realConnection;
                        j3 = j2;
                    }
                    j2 = j3;
                    realConnection = realConnection3;
                    i2 = i3;
                } else {
                    i++;
                }
            }
            if (j2 >= this.ʼ || i2 > this.ˎ) {
                this.ʻ.remove(realConnection);
                ॱ(realConnection);
                fvg.ˎ(realConnection.socket());
                return 0;
            } else if (i2 > 0) {
                j3 = this.ʼ - j2;
                return j3;
            } else if (i > 0) {
                j3 = this.ʼ;
                return j3;
            } else {
                this.ˋ = false;
                return -1;
            }
        }
    }

    private int ˊ(RealConnection realConnection, long j) {
        List list = realConnection.allocations;
        int i = 0;
        while (i < list.size()) {
            Reference reference = (Reference) list.get(i);
            if (reference.get() != null) {
                i++;
            } else {
                e eVar = (e) reference;
                fwy.ˋ().ˎ("A connection to " + realConnection.route().address().ˊ() + " was leaked. Did you forget to close a response body?", eVar.ˋ);
                list.remove(i);
                realConnection.noNewStreams = true;
                if (list.isEmpty()) {
                    realConnection.idleAtNanos = j - this.ʼ;
                    return 0;
                }
            }
        }
        return list.size();
    }

    private fuq ˊ(ftt o_ftt) {
        for (fuq o_fuq : this.ʽ) {
            if (o_ftt.equals(o_fuq.ॱ())) {
                return o_fuq;
            }
        }
        return null;
    }

    private RealConnection ॱ(ftt o_ftt) {
        fuq ˊ = ˊ(o_ftt);
        if (ˊ != null) {
            return ˊ.ˏ();
        }
        return null;
    }

    public synchronized void ˋ(RealConnection realConnection) {
        fuq ˊ = ˊ(realConnection.route().address());
        if (ˊ == null) {
            ˊ = new fuq(realConnection.route().address());
            this.ʽ.push(ˊ);
        }
        ˊ.ˏ(realConnection);
    }

    private void ॱ(RealConnection realConnection) {
        if (realConnection != null && realConnection.isMultiplexed()) {
            fuq ˊ = ˊ(realConnection.route().address());
            if (ˊ != null) {
                ˊ.ˎ(realConnection);
                if (ˊ.ˋ()) {
                    this.ʽ.remove(ˊ);
                    ˎ(realConnection.route().address());
                }
            }
        }
    }

    public synchronized int ˏ(ftt o_ftt) {
        int i;
        i = 0;
        for (RealConnection realConnection : this.ʻ) {
            int i2;
            if (o_ftt.equals(realConnection.route().address) && !realConnection.noNewStreams && realConnection.isMultiplexed() && (realConnection.successCount == 0 || realConnection.isHealthy(true))) {
                i2 = i + 1;
            } else {
                i2 = i;
            }
            i = i2;
        }
        return i;
    }

    public synchronized int ˎ(String str, int i, String str2) {
        int i2;
        i2 = 0;
        for (RealConnection realConnection : this.ʻ) {
            int i3;
            if (realConnection.isMultiplexed() && str.equals(realConnection.route().address.ˊ().ʻ()) && i == realConnection.route().address.ˊ().ॱॱ() && str2.equals(realConnection.route().address.ˊ().ˋ()) && !realConnection.noNewStreams && (realConnection.successCount == 0 || realConnection.isHealthy(true))) {
                i3 = i2 + 1;
            } else {
                i3 = i2;
            }
            i2 = i3;
        }
        return i2;
    }

    public synchronized boolean ॱ(String str, int i, String str2) {
        boolean z;
        for (RealConnection realConnection : this.ʻ) {
            if (realConnection.isMultiplexed() && str.equals(realConnection.route().address.ˊ().ʻ()) && i == realConnection.route().address.ˊ().ॱॱ() && str2.equals(realConnection.route().address.ˊ().ˋ()) && !realConnection.noNewStreams && realConnection.isHealthy(true)) {
                realConnection.keepaliveTimestampNs = System.nanoTime();
                z = true;
                break;
            }
        }
        z = false;
        return z;
    }

    private void ˎ(ftt o_ftt) {
        fur ˊ = o_ftt.ˊ();
        Iterator it = this.ॱॱ.iterator();
        while (it.hasNext()) {
            d dVar = (d) ((WeakReference) it.next()).get();
            if (dVar != null) {
                dVar.ॱ(ˊ.ʻ(), ˊ.ॱॱ(), ˊ.ˋ());
            } else {
                it.remove();
            }
        }
    }

    public synchronized void ˋ(d dVar) {
        if (dVar != null) {
            this.ॱॱ.add(new WeakReference(dVar));
        }
    }
}
